
/**获取当前时间*/
const getCurrentTime = function (isFormated, now) {
  let isFormat = isFormated || true;
  let keep = '';
  let date = now || new Date();
  let y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  let rand = Math.round(Math.random() * 899 + 100);
  if (isFormat) {
    keep = y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s; // 2016-06-14 13:49:47
  } else {
    keep = y + '' + m + '' + d + '' + h + '' + f + '' + s; // 20160614134947
  }
  return keep;
}

/**获取当前时间戳*/
const getCurrentTimestamp = function () {
  let now = getCurrentTime(true, new Date());
  return datetime2timestamp(now);
}

/**时间戳转格式化字符串*/
const timestampToFormated = function (timestamp) {
  let date = null;
  //  if(timestamp.toString().length == 10){
  //    date = new Date(timestamp*1000);//如果date为10位不需要乘1000
  //  }else if(timestamp.toString().length > 10){
  date = new Date(timestamp);
  //  }else{
  //    return "";
  //  }
  return getCurrentTime(true, date);
  // let Y = date.getFullYear() + '-';
  // let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  // let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  // let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  // let m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  // let s = (date.getSeconds() <10 ? '0' + date.getSeconds() : date.getSeconds());
  // return Y+M+D+h+m+s;
}

/**格式化字符串转时间戳*/
const datetime2timestamp = function (datetime) {
  let now = new Date(datetime);
  return now.getTime() / 1000;
}

const encryption = function (type, code, card) {
  let codeStr = code.split('');
  let codeLength = codeStr.length;
  let newStrCode = {};
  let paymentCode = '';
  let newpaymentCode = '';
  let keep = '';
  let date = new Date();
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  keep = h + '' + f + '' + s;
  paymentCode = type + '' + card + '' + keep;
  // console.log("keep======>"+ keep);
  // console.log("转前号======>"+ paymentCode);
  let newStr = paymentCode.substring(paymentCode.length - codeLength, paymentCode.length);
  newStrCode = newStr.split('');
  let supreNew = newStrCode;
  for (let index in codeStr) {
    let value = codeStr[index];
    let transSafeCode = newStrCode[index];
    let transPaymentCode = newStrCode[value - 1];
    newStrCode[index] = transPaymentCode;
    newStrCode[value - 1] = transSafeCode;
  }
  let strCode = newStrCode.join('');
  newpaymentCode = paymentCode.substring(0, paymentCode.length - 5) + '' + strCode;
  // console.log("转后号======>"+ newpaymentCode);
  return newpaymentCode;
}

Date.prototype.format = function (format) {
  var date = {
    "M+": this.getMonth() + 1,
    "d+": this.getDate(),
    "h+": this.getHours(),
    "m+": this.getMinutes(),
    "s+": this.getSeconds(),
    "q+": Math.floor((this.getMonth() + 3) / 3),
    "S+": this.getMilliseconds()
  };
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (var k in date) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
        date[k] : ("00" + date[k]).substr(("" + date[k]).length));
    }
  }
  return format;
}
/**
 * 判断日期是否在一个时间段内
 * @param {*} d1 开始时间, 格式按js可以用new Date()创建出有效日期的方式传入
 * @param {*} d2 结束时间, 格式按js可以用new Date()创建出有效日期的方式传入
 */
function nowInDateBetwen(d1, d2) {
  //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
  // var dateBegin = new Date(d1.replace(/-/g, "/"));//将-转化为/，使用new Date
  // var dateEnd = new Date(d2.replace(/-/g, "/"));//将-转化为/，使用new Date
  var dateBegin = new Date(d1); //将-转化为/，使用new Date
  var dateEnd = new Date(d2); //将-转化为/，使用new Date
  var dateNow = new Date(); //获取当前时间

  var beginDiff = dateNow.getTime() - dateBegin.getTime(); //时间差的毫秒数       
  var beginDayDiff = Math.floor(beginDiff / (24 * 3600 * 1000)); //计算出相差天数

  var endDiff = dateEnd.getTime() - dateNow.getTime(); //时间差的毫秒数
  var endDayDiff = Math.floor(endDiff / (24 * 3600 * 1000)); //计算出相差天数       
  if (endDayDiff < 0) { //已过期
    return false
  }
  if (beginDayDiff < 0) { //没到开始时间
    return false;
  }
  return true;
}

function rpxToPx(rpxNum, systemInfo) {
  if (!systemInfo) {
    console.error('请传入systemInfo');
    return;
  }
  let radio = 750 / systemInfo.windowWidth;
  return rpxNum / radio;
}

function pxToRpx(pxNum, systemInfo) {
  if (!systemInfo) {
    console.error('请传入systemInfo');
    return;
  }
  let radio = 750 / systemInfo.windowWidth;
  return pxNum * radio;
}

/**
 * 转化日期格式
 * @param {原始日期} date 
 * @param {*} 日期格式:
 *                例子:yyyy-MM-dd, yyyy-MM-dd HH:
 */
const fortmatDate = function (date, fmt) {
  let parmDate = new Date(date).format(fmt);
  return parmDate;
}

module.exports = {
  getCurrentTime: getCurrentTime,
  timestampToFormated: timestampToFormated,
  datetime2timestamp: datetime2timestamp,
  getCurrentTimestamp: getCurrentTimestamp,
  strSubstring: strSubstring,
  encryption: encryption,
  transitionend: transitionend,
  excuteDotAnimation: excuteDotAnimation,
  nowInDateBetwen: nowInDateBetwen,
  rpxToPx: rpxToPx,
  pxToRpx: pxToRpx,
  fortmatDate: fortmatDate,
  requestSubscribeMessage: requestSubscribeMessage,
  uuid: uuid,
  readMallMenuWithKey: readMallMenuWithKey,
  readMallMenuWithKeys: readMallMenuWithKeys,
  makePickUpPreciseTimeArray: makePickUpPreciseTimeArray,
}